/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.config.Config;
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class CustomerFieldManager
/*     */ {
/*  21 */   private static Logger logger = Logger.getLogger(CustomerFieldManager.class
/*  22 */     .getName());
/*     */ 
/*     */   public List<Map<String, Object>> listAll(int companyId)
/*     */     throws DBException, SQLException
/*     */   {
/*  34 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  35 */     if (conn == null) {
/*  36 */       throw new DBException();
/*     */     }
/*     */ 
/*  39 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  41 */       return (List)runner.query(conn, "SELECT CustomerFieldId, CustomerFieldName, FieldType, ValueType, InitValue FROM T_Customer_Field WHERE CompanyId = ?  ORDER BY OrderNum ASC", 
/*  42 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */     } finally {
/*     */       try {
/*  45 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  47 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> list(int companyId, int type)
/*     */   {
/*  62 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  63 */     if (conn == null) {
/*  64 */       return null;
/*     */     }
/*     */ 
/*  67 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/*     */       List localList;
/*  69 */       if (type == 0) {
/*  70 */         return (List)runner.query(conn, "SELECT CustomerFieldId, CustomerFieldName, FieldType, ValueType, InitValue FROM T_Customer_Field WHERE CompanyId = ?  ORDER BY OrderNum ASC", 
/*  71 */           new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */       }
/*  73 */       return (List)runner.query(conn, "SELECT CustomerFieldId, CustomerFieldName, FieldType, ValueType, InitValue FROM T_Customer_Field WHERE CompanyId = ?  AND (FieldType = ? OR FieldType = ?) ORDER BY OrderNum ASC", 
/*  74 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId), Integer.valueOf(type), 
/*  75 */         Integer.valueOf(0) });
/*     */     } catch (SQLException e) {
/*  77 */       logger.warn("get customer field info failed", e);
/*     */     } finally {
/*     */       try {
/*  80 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  82 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*  85 */     return null;
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> list(int companyId, int customerId, int type)
/*     */   {
/* 100 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 101 */     if (conn == null) {
/* 102 */       return null;
/*     */     }
/*     */ 
/* 105 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 107 */       return (List)runner.query(conn, "SELECT Type, Info1 FROM T_Customer_Ext_Info WHERE CustomerId = ? AND Type = ? AND CompanyId = ? ", 
/* 108 */         new MyMapListHandler(), new Object[] { Integer.valueOf(customerId), Integer.valueOf(type), Integer.valueOf(companyId) });
/*     */     } catch (SQLException e) {
/* 110 */       logger.warn("get customer field info failed", e);
/*     */     } finally {
/*     */       try {
/* 113 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 115 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 118 */     return null;
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> listAllMin(int companyId)
/*     */     throws DBException, SQLException
/*     */   {
/* 132 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 133 */     if (conn == null) {
/* 134 */       throw new DBException();
/*     */     }
/*     */ 
/* 137 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 139 */       return (List)runner.query(conn, "SELECT CustomerFieldId, CustomerFieldName,ValueType,FieldType FROM T_Customer_Field WHERE CompanyId = ? ORDER BY OrderNum ASC", 
/* 140 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */     } finally {
/*     */       try {
/* 143 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 145 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int add(int companyId, String name, int fieldType, int valueType, String initValue, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/* 165 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 166 */     if (conn == null) {
/* 167 */       throw new DBException();
/*     */     }
/* 169 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 170 */       new ScalarHandler(), new String[] { "CustomerFieldId" });
/*     */     try {
/* 172 */       runner.update(conn, "INSERT INTO T_Customer_Field (CustomerFieldName, FieldType, ValueType, InitValue, CompanyId, CreateDate, CreateUserId) values (?, ?, ?, ?, ?, NOW(), ?)", new Object[] { name, Integer.valueOf(fieldType), 
/* 173 */         Integer.valueOf(valueType), initValue, Integer.valueOf(companyId), userId });
/* 174 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*     */     } finally {
/*     */       try {
/* 177 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 179 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int update(int customerFieldId, String name, int fieldType, int valueType, String initValue, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/* 200 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 201 */     if (conn == null) {
/* 202 */       throw new DBException();
/*     */     }
/* 204 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 207 */       return runner.update(conn, "UPDATE T_Customer_Field SET CustomerFieldName = ? , FieldType = ?, ValueType = ?, InitValue = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE CustomerFieldId = ?", new Object[] { name, 
/* 208 */         Integer.valueOf(fieldType), Integer.valueOf(valueType), initValue, userId, Integer.valueOf(customerFieldId) });
/*     */     } finally {
/*     */       try {
/* 211 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 213 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int delete(int customerFieldId)
/*     */     throws DBException, SQLException
/*     */   {
/* 227 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 228 */     if (conn == null) {
/* 229 */       throw new DBException();
/*     */     }
/* 231 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 234 */       int result = runner.update(conn, "DELETE FROM T_Customer_Field WHERE CustomerFieldId = ?", 
/* 235 */         Integer.valueOf(customerFieldId));
/*     */ 
/* 238 */       runner.update(conn, "DELETE FROM T_Customer_Ext_Info WHERE Type = ?", 
/* 239 */         Integer.valueOf(customerFieldId));
/* 240 */       return result;
/*     */     } finally {
/*     */       try {
/* 243 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 245 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void init(int companyId, String userId) throws DBException, SQLException
/*     */   {
/* 252 */     Config config = Config.getInstance();
/* 253 */     String companyCustomerFields = config.companyCustomerField;
/*     */ 
/* 255 */     if ((companyCustomerFields == null) || 
/* 256 */       (companyCustomerFields.trim().length() == 0)) {
/* 257 */       return;
/*     */     }
/*     */ 
/* 260 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 261 */     if (conn == null) {
/* 262 */       throw new DBException();
/*     */     }
/* 264 */     QueryRunner runner = new QueryRunner();
/* 265 */     String[] arrFields = companyCustomerFields.split(",");
/*     */     try {
/* 267 */       for (String field : arrFields) {
/* 268 */         if ((field != null) && (field.trim().length() != 0))
/*     */         {
/* 271 */           runner.update(conn, "INSERT INTO T_Customer_Field (CustomerFieldName, FieldType, ValueType, InitValue, CompanyId, CreateDate, CreateUserId) values (?, ?, ?, ?, ?, NOW(), ?)", new Object[] { field, 
/* 272 */             Integer.valueOf(1), 
/* 273 */             Integer.valueOf(0), null, Integer.valueOf(companyId), userId });
/*     */         }
/*     */       }
/* 276 */       String peopleCustomerFields = config.peopleCustomerField;
/* 277 */       if ((peopleCustomerFields == null) || 
/* 278 */         (peopleCustomerFields.trim().length() == 0)) {
/* 279 */         return;
/*     */       }
/* 281 */       arrFields = peopleCustomerFields.split(",");
/* 282 */       for (String field : arrFields)
/* 283 */         if ((field != null) && (field.trim().length() != 0))
/*     */         {
/* 287 */           runner.update(conn, "INSERT INTO T_Customer_Field (CustomerFieldName, FieldType, ValueType, InitValue, CompanyId, CreateDate, CreateUserId) values (?, ?, ?, ?, ?, NOW(), ?)", new Object[] { field, 
/* 288 */             Integer.valueOf(2), 
/* 289 */             Integer.valueOf(0), null, Integer.valueOf(companyId), userId });
/*     */         }
/*     */     } finally {
/*     */       try {
/* 293 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 295 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */     try
/*     */     {
/* 293 */       DbUtils.close(conn);
/*     */     } catch (SQLException e) {
/* 295 */       logger.warn("close conn failed", e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public int updateSort(int id, int order, String userId)
/*     */   {
/* 310 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 311 */     if (conn == null) {
/* 312 */       return 0;
/*     */     }
/* 314 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 317 */       return runner.update(conn, "UPDATE T_Customer_Field SET OrderNum = ? , UpdateDate = NOW(), UpdateUserId = ? WHERE CustomerFieldId = ?", new Object[] { 
/* 318 */         Integer.valueOf(order), userId, Integer.valueOf(id) });
/*     */     } catch (SQLException e) {
/* 320 */       logger.warn("update order failed", e);
/*     */     } finally {
/*     */       try {
/* 323 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 325 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 328 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.CustomerFieldManager
 * JD-Core Version:    0.6.1
 */